#include "cobra11syndicate.hlsli"
#include "..\\standard.hlsli"
//	// Parameters:
//	//
//	//   sampler2D DiffuseTex1;
//	//   sampler2D DiffuseTex2;
//	//   sampler2D DiffuseTex3;
//	//   float3 DitherClipParams;
//	//   bool DitherFlag;
//	//   sampler2D DitherTex;
//	//   float4 DitherTexParams;
//	//   float4 Exposure;
//	//   float3 FogColor;
//	//   float3 LightVec;
//	//   float4 SelCascade;
//	//   sampler2D ShadowMap;
//	//   float4x3 ShadowMats[4];
//	//   float3x3 ShadowNormalMat;
//	//   float4 ShadowNormalOffset;
//	//   bool ShadowsFlag;
//	//   float3 SunColor;
//	//
//	//
//	// Registers:
//	//
//	//   Name               Reg   Size
//	//   ------------------ ----- ----
//	//   ShadowsFlag        b1       1
//	//   DitherFlag         b4       1
//	//   LightVec           c14      1
//	//   SunColor           c15      1
//	//   ShadowNormalMat    c17      3
//	//   DitherTexParams    c20      1
//	//   DitherClipParams   c21      1
//	//   ShadowNormalOffset c22      1
//	//   Exposure           c23      1
//	//   FogColor           c29      1
//	//   SelCascade         c35      1
//	//   ShadowMats         c36     12
//	//   DiffuseTex1        s0       1
//	//   DiffuseTex2        s2       1
//	//   DiffuseTex3        s4       1
//	//   ShadowMap          s7       1
//	//   DitherTex          s15      1

float4 ps_main(
	float4 vPos : SV_Position /* dcl vPos.xy */,
	float4 VertexColor : COLOR0 /* dcl_color v0.xyw */,
	float4 v1 : TEXCOORD8 /* dcl_texcoord8 v1 */,
	float3 normal : TEXCOORD /* dcl_texcoord v2.xyz */,
	float4 distanceToCamera : TEXCOORD1 /* dcl_texcoord1 v3 */,
	float4 v4 : TEXCOORD2 /* dcl_texcoord2 v4 */,
	float4 v5 : TEXCOORD4 /* dcl_texcoord4 v5.xy */,
	float4 v44 : TEXCOORD6 /* dcl_texcoord4 v5.xy */,
	float3 v6 : TEXCOORD7 /* dcl_texcoord7 v6.xyz */
	) : SV_TARGET
{
	//return psIn.VertexColor.rgba;
	//return float4(psIn.diffuseColor.rgb, 1.0f);
	float4 colorOut = float4(0, 0, 0, 0);

	float4 c0 = float4(8, -3.5f, 1, 0); // def c0, 8, -3.5, 1, 0
	float4 c1 = float4(0.308600008, 0.609399974, 0.0820000023, 3); // def c1, 0.308600008, 0.609399974, 0.0820000023, 3
	//float4 ambientColor = float4(0.1, 0.1, 0.1, 3); // def c1, 0.308600008, 0.609399974, 0.0820000023, 3
	float4 c2 = float4 (1, 2, 3, 4); // def c2, 1, 2, 3, 4
	float4 c3 = float4(1, 0, -1, -2); // def c3, 1, 0, -1, -2
	float4 c4 = float4(0.800000012, 0.5, 0, 0); // def c4, 0.800000012, 0.5, 0, 0

	float4 oC0 = (float4)0;
	float4 r0 = (float4)0;
	float4 r1 = (float4)0;
	float4 r2 = (float4)0;
	float4 r3 = (float4)0;
	float4 r4 = (float4)0;
	float4 r5 = (float4)0;
	float4 r6 = (float4)0;
	float4 r7 = (float4)0;


	r0.xyz = normalize(normal.xyz);//nrm r0.xyz, v2
	r0.w = saturate(dot(r0.xyz, LightVec.xyz));//dp3_sat r0.w, r0, c14
	r1.x = -r0.w + c0.z;//add r1.x, -r0.w, c0.z
	r1.y = r1.x * r1.x;//mul r1.y, r1.x, r1.x
	r1.x = r1.x * -r1.y + c0.z;//mad r1.x, r1.x, -r1.y, c0.z
	r0.w = dot(float2(r1.x, r1.x), float2(r0.w, r0.w)) + c0.w;//dp2add_pp r0.w, r1.x, r0.w, c0.w
	r1.x = dot(distanceToCamera.xyz, distanceToCamera.xyz);//dp3 r1.x, v3, v3
	r1.x = rsqrt(r1.x);//rsq r1.x, r1.x
	if (false)
	{ //if b4
		r1.y = 1.0f / r1.x;//rcp r1.y, r1.x
		r1.zw = vPos.xy * DitherTexParams.xy + DitherTexParams.xy;//mad r1.zw, vPos.xyxy, c20.xyxy, c20
		r2.xyzw = TexDither.Sample(DitherTex, r1.zw);//texld r2, r1.zwzw, s15
		r1.z = r2.w * DitherClipParams.y;//mul r1.z, r2.w, c21.y
		r1.y = r1.y * DitherClipParams.x + r1.z;//mad r1.y, r1.y, c21.x, r1.z
		r2.xyzw = r1.y + DitherClipParams.z;//add r2, r1.y, c21.z
		clip(r2.xyzw);//texkill r2
	} //endif
	r1.xyz = r1.x * distanceToCamera.xyz;//mul_pp r1.xyz, r1.x, v3
	r1.x = dot(r0.xyz, r1.xyz);//dp3_pp r1.x, r0, r1
	r1.x = saturate(-r1.x + c0.z);//add_sat_pp r1.x, -r1.x, c0.z
	r1.x = r1.x * r1.x;//mul r1.x, r1.x, r1.x
	r1.x = r1.x * c4.x + c4.y;//mad r1.x, r1.x, c4.x, c4.y
	r0.w = r0.w * r1.x;//mul_pp r0.w, r0.w, r1.x
	//return float4(r0.www, 1);
	r1.xyz = saturate(VertexColor.xyw * c0.x + c0.y);//mad_sat r1.xyz, v0.xyww, c0.x, c0.y
	//return float4(r1.xyz, 1);
	if (false)
	{ //if b1
		r1.w = v6.z * SelCascade.x + SelCascade.y;//mad r1.w, v6.z, c35.x, c35.y
		r1.w = log(r1.w);//log r1.w, r1.w
		r1.w = r1.w * SelCascade.z + SelCascade.w;//mad r1.w, r1.w, c35.z, c35.w
		r2.x = max(r1.w, c0.w);//max r2.x, r1.w, c0.w
		r1.w = min(r2.x, c1.w);//min r1.w, r2.x, c1.w
		r2.x = frac(r1.w);//frc r2.x, r1.w
		r2.x = r1.w + -r2.x;//add r2.x, r1.w, -r2.x
		r3.xyzw = r1.w + c3.xyzw;//add r3, r1.w, c3
		r4.xyzw = -r1.w + c2.xyzw;//add r4, -r1.w, c2
		r5.xyzw = min(r3.xyzw, r4.xyzw);//min r5, r3, r4
		r3.xyzw = max(r5.xyzw, c0.w);//max r3, r5, c0.w
		r1.w = dot(r3.xyzw, ShadowNormalOffset.xyzw);// dp4 r1.w, r3, c22
		r3.x = dot(r0.xyz, ShadowNormalMat._m00_m10_m20);//dp3 r3.x, r0, c17
		r3.y = dot(r0.xyz, ShadowNormalMat._m01_m11_m21);//dp3 r3.y, r0, c18
		r3.z = dot(r0.xyz, ShadowNormalMat._m02_m12_m22);//dp3 r3.z, r0, c19
		r3.xyz = r3.xyz * r1.w + v6.xyz;//mad r3.xyz, r3, r1.w, v6
		r0.x = r2.x + -c2.y;//add r0.x, r2.x, -c2.y
		r3.w = c0.z;//mov_pp r3.w, c0.z
		r4.xyzw = ShadowMats[0]._m02_m12_m22_m32;//mov r4, c38
		r4.xyzw = float4(-abs(r2.x) < 0 ? r4.x : ShadowMats[1]._m02,
			-abs(r2.x) < 0 ? r4.y : ShadowMats[1]._m12,
			-abs(r2.x) < 0 ? r4.z : ShadowMats[1]._m22,
			-abs(r2.x) < 0 ? r4.w : ShadowMats[1]._m32);
		//cmp r4, -r2_abs.x, r4, c41
		r5.xyzw = ShadowMats[2]._m02_m12_m22_m32;//mov r5, c44
		r5.xyzw = float4(-r0.x < 0 ? r5.x : ShadowMats[3]._m02,
			-r0.x < 0 ? r5.y : ShadowMats[3]._m12,
			-r0.x < 0 ? r5.z : ShadowMats[3]._m22,
			-r0.x < 0 ? r5.w : ShadowMats[3]._m32);
		//cmp r5, -r0.x, r5, c47
		r4.xyzw = float4(r0.x < 0 ? r5.x : r4.x,
			r0.x < 0 ? r5.y : r4.y,
			r0.x < 0 ? r5.z : r4.z,
			r0.x < 0 ? r5.w : r4.w);
		//cmp r4, r0.x, r5, r4
		r4.z = dot(r3.xyzw, r4.xyzw);// dp4 r4.z, r3, r4
		r5.xyzw = ShadowMats[0]._m00_m10_m20_m30;//mov r5, c36
		r5.xyzw = float4(-abs(r2.x) < 0 ? r5.x : ShadowMats[1]._m00,
			-abs(r2.x) < 0 ? r5.y : ShadowMats[1]._m10,
			-abs(r2.x) < 0 ? r5.z : ShadowMats[1]._m20,
			-abs(r2.x) < 0 ? r5.w : ShadowMats[1]._m30);
		//cmp r5, -r2_abs.x, r5, c39
		r6.xyzw = ShadowMats[2]._m00_m10_m20_m30;//mov r6, c42
		r6.xyzw = float4(-r0.x < 0 ? r6.x : ShadowMats[3]._m00,
			-r0.x < 0 ? r6.y : ShadowMats[3]._m10,
			-r0.x < 0 ? r6.z : ShadowMats[3]._m20,
			-r0.x < 0 ? r6.w : ShadowMats[3]._m30);
		//cmp r6, -r0.x, r6, c45
		r5.xyzw = float4(r0.x < 0 ? r6.x : r5.x,
			r0.x < 0 ? r6.y : r5.y,
			r0.x < 0 ? r6.z : r5.z,
			r0.x < 0 ? r6.w : r5.w);
		//cmp r5, r0.x, r6, r5
		r4.x = dot(r3.xyzw, r5.xyzw);// dp4 r4.x, r3, r5
		r5.xyzw = ShadowMats[0]._m01_m11_m21_m31;//mov r5, c37
		r2.xyzw = float4(-abs(r2.x) < 0 ? r5.x : ShadowMats[1]._m01,
			-abs(r2.x) < 0 ? r5.y : ShadowMats[1]._m11,
			-abs(r2.x) < 0 ? r5.z : ShadowMats[1]._m21,
			-abs(r2.x) < 0 ? r5.w : ShadowMats[1]._m31);
		//cmp r2, -r2_abs.x, r5, c40
		r5.xyzw = ShadowMats[2]._m01_m11_m21_m31;//mov r5, c43
		r5.xyzw = float4(-r0.x < 0 ? r5.x : ShadowMats[3]._m01,
			-r0.x < 0 ? r5.y : ShadowMats[3]._m11,
			-r0.x < 0 ? r5.z : ShadowMats[3]._m21,
			-r0.x < 0 ? r5.w : ShadowMats[3]._m31);
		//cmp r5, -r0.x, r5, c46
		r2.xyzw = float4(r0.x < 0 ? r5.x : r2.x,
			r0.x < 0 ? r5.y : r2.y,
			r0.x < 0 ? r5.z : r2.z,
			r0.x < 0 ? r5.w : r2.w);
		//cmp r2, r0.x, r5, r2
		r4.y = dot(r3.xyzw, r2.xyzw);// dp4 r4.y, r3, r2
		r4.w = c0.z;//mov_pp r4.w, c0.z
		r2.xyzw = TexShadowMap.Sample(ShadowMap, r4.xy / r4.w);//texldp r2, r4, s7
		r2.w = r2.x;//mov r2.w, r2.x
	}
	else
	{ //else
		r2.w = -r1.z + c0.z;//add r2.w, -r1.z, c0.z
	} //endif
	r3.xyzw = Tex1.Sample(DiffuseTex1, v4.xy);//texld r3, v4, s0
	r0.x = dot(r3.xyz, c1.xyz);//dp3 r0.x, r3, c1
	r4.xyz = lerp(r3.xyz, r0.x, Exposure.w);//lrp r4.xyz, c23.w, r0.x, r3
	r4.w = r3.w;//mov r4.w, r3.w
	r3.xyzw = Tex2.Sample(DiffuseTex2, v4.zw);//texld r3, v4.zwzw, s2
	r0.x = dot(r3.xyz, c1.xyz);//dp3 r0.x, r3, c1
	r5.xyz = lerp(r3.xyz, r0.x, Exposure.w);//lrp r5.xyz, c23.w, r0.x, r3
	r5.w = r3.w;//mov r5.w, r3.w
	r3.xyzw = lerp(r4.xyzw, r5.xyzw, r1.x);//lrp r3, r1.x, r5, r4
	r4.xyzw = Tex3.Sample(DiffuseTex3, v5.xy);//texld r4, v5, s4
	r0.x = dot(r4.xyz, c1.xyz);//dp3 r0.x, r4, c1
	r5.xyz = lerp(r4.xyz, r0.x, Exposure.w);//lrp r5.xyz, c23.w, r0.x, r4
	r0.x = r1.y * r4.w;//mul r0.x, r1.y, r4.w
	r5.w = r4.w;//mov r5.w, r4.w
	r1.xyzw = lerp(r3.xyzw, r5.xyzw, r0.x);//lrp r1, r0.x, r5, r3
	r2.xyz = r2.w * SunColor.xyz;//mul_pp r2.xyz, r2.w, c15
	//return float4(r2.xyz, 1);
	r0.xyzw = r0.w * r2.xyzw + diffuseColor.xyzw;//mad_pp r0, r0.w, r2, v1
	//return float4(r0.xyz,1) ;
	r0.xyz = r1.xyz * r0.xyz + -FogColor;//mad r0.xyz, r1, r0, -c29
	//return float4(r0.xyz, 1);
	oC0.xyz = distanceToCamera.w * r0.xyz + FogColor.xyz;//mad_pp oC0.xyz, v3.w, r0, c29
	r0.x = r0.w * r1.w;//mul_pp r0.x, r0.w, r1.w
	oC0.w = 1;// r0.x;//mov_pp oC0.w, r0.x


	return oC0;

	// Notizen 08122015
	// texCoord5.xy geht .zw nicht.
}

//	ps_3_0
//		dcl_color v0.xyw
//		dcl_texcoord8 v1
//		dcl_texcoord v2.xyz
//		dcl_texcoord1 v3
//		dcl_texcoord2 v4
//		dcl_texcoord4 v5.xy
//		dcl_texcoord7 v6.xyz
//		dcl vPos.xy
//		dcl_2d s0
//		dcl_2d s2
//		dcl_2d s4
//		dcl_2d s7
//		dcl_2d s15

//		if b4 // Dither 
//			rcp r1.y, r1.x
//			mad r1.zw, vPos.xyxy, c20.xyxy, c20
//			texld r2, r1.zwzw, s15
//			mul r1.z, r2.w, c21.y
//			mad r1.y, r1.y, c21.x, r1.z
//			add r2, r1.y, c21.z
//			texkill r2
//		endif // End dither

//
//			// approximately 95 instruction slots used (5 texture, 90 arithmetic)
//};